package com.yanqu.road.dao.impl.activity.share;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserShareData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ShareDataDaoImpl extends TempDataObjectDao<UserShareData> {
    public static String getUserShareDataSql = "select * from `t_u_invited_data` where `userId`=?";
    public UserShareData getUserShareData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserShareDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserShareDataSql = "update `t_u_invited_data` set shareCode=?,todayShareTimes=?,nextShareTime=?,lastResetTime=?,invitedServerId=?,invitedUserId=?,invitedCode=?,nextInvitationTime=? where userId=?";
    @Override
    protected boolean updateData(UserShareData userShareData) {
        userShareData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, userShareData.getShareCode()));
        para.put(i++, new DbParameter(Types.INTEGER, userShareData.getTodayShareTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getNextShareTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userShareData.getInvitedServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getInvitedUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userShareData.getInvitedCode()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getNextInvitationTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getUserId()));
        if (getDb().executeNoneQuery(updateUserShareDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserShareDataSql = "insert into `t_u_invited_data`(userId,shareCode,todayShareTimes,nextShareTime,lastResetTime,invitedServerId,invitedUserId,invitedCode,nextInvitationTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserShareData userShareData) {
        userShareData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userShareData.getShareCode()));
        para.put(i++, new DbParameter(Types.INTEGER, userShareData.getTodayShareTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getNextShareTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userShareData.getInvitedServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getInvitedUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userShareData.getInvitedCode()));
        para.put(i++, new DbParameter(Types.BIGINT, userShareData.getNextInvitationTime()));
        if (getDb().executeNoneQuery(insertUserShareDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserShareData parseData(ResultSet rs) throws SQLException {
        UserShareData userShareData = new UserShareData();
        userShareData.setUserId(rs.getLong("userId"));
        userShareData.setShareCode(rs.getString("shareCode"));
        userShareData.setTodayShareTimes(rs.getInt("todayShareTimes"));
        userShareData.setNextShareTime(rs.getLong("nextShareTime"));
        userShareData.setLastResetTime(rs.getLong("lastResetTime"));
        userShareData.setInvitedServerId(rs.getInt("invitedServerId"));
        userShareData.setInvitedUserId(rs.getLong("invitedUserId"));
        userShareData.setInvitedCode(rs.getString("invitedCode"));
        userShareData.setNextInvitationTime(rs.getLong("nextInvitationTime"));
        return userShareData;
    }


    public static String getAllCodeSql = "select userId, shareCode from `t_u_invited_data`";
    public Map<Long,String> getAllCode() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, String> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAllCodeSql);
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                String shareCode = rs.getString("shareCode");
                if (StringUtils.isNullOrEmpty(shareCode)) {
                    continue;
                }
                dataMap.put(userId, shareCode);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

}
